package com.hisicom.scqxspxcjc.base.exception;

import com.hisicom.scqxspxcjc.base.bean.Result;
import com.hisicom.scqxspxcjc.base.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * @author zhonggls
 * @date 2019/11/5 10:26
 * @Explain 自定义异常类接口
 */

@ControllerAdvice
public class ControllerAdviceProcessor {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected MessageSource messageSource;

    @ExceptionHandler({RuntimeException.class})
    @ResponseBody
    public Object handleException(Exception ex) {
        String msgCode = ex.getMessage();
        try {
            Integer code = Integer.parseInt(msgCode);
            logger.error(ex.toString()+ ResultEnum.getByKey(code).getMsg());
            return Result.error(ResultEnum.getByKey(code));
        } catch (Exception e) {
            logger.error(ex.toString());
            if(ex.getMessage()==null){
                return Result.error(ex.toString());
            }else{
                return Result.error(ex.getMessage());
            }
        }
    }

}
